<template>
  <el-row>
<!--    <el-col :span="4">-->
<!--      <div class="box">-->
<!--        <el-scrollbar>-->
<!--          <basic-container>-->
<!--            <avue-tree :option="treeOption" :data="treeData" @node-click="nodeClick"/>-->
<!--          </basic-container>-->
<!--        </el-scrollbar>-->
<!--      </div>-->
<!--    </el-col>-->
    <el-col :span="24">
      <basic-container>
        <avue-crud :option="option"
                   :table-loading="loading"
                   :data="data"
                   :page="page"
                   :permission="permissionList"
                   :before-open="beforeOpen"
                   v-model="form"
                   ref="crud"
                   @row-update="rowUpdate"
                   @row-save="rowSave"
                   @row-del="rowDel"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @selection-change="selectionChange"
                   @current-change="currentChange"
                   @size-change="sizeChange"
                   @on-load="onLoad">
          <template slot="menuLeft">
            <el-button type="danger"
                       size="small"
                       icon="el-icon-delete"
                       plain
                       v-if="permission.nj_delete"
                       @click="handleDelete">删 除
            </el-button>
          </template>
        </avue-crud>
      </basic-container>
    </el-col>
  </el-row>
</template>

<script>
import {getList, getDetail, add, update, remove, getBjTree, course} from "@/api/service/gradeCourses";
import {mapGetters} from "vuex";
import {getStore} from "@/util/store";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      treeData: [],
      treeOption: {
        nodeKey: 'id',
        lazy: false,
        addBtn: false,
        menu: false,
        size: 'small',
        defaultExpandAll: true,
        props: {
          labelText: '标题',
          label: 'title',
          value: 'value',
          children: 'children'
        }
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "学校名称",
            prop: "schoolId",
            search: true,
            hide:false,
            rules: [{
              required: true,
              message: "请输入学校名称",
              trigger: "blur"
            }],
            type: "tree",
            dicUrl: "/api/PcPublicService/GetSchoolTreeSelect",
            cascader: ['kcId'],
            props: {
              label: "xxName",
              value: "id"
            },
            change: ({value}) => {
              this.schoolTypeChange(value);
            }
          },
          {
            label: "所属阶段",
            prop: "jdId",
            search: true,
            rules: [{
              required: true,
              message: "请选择所属阶段",
              trigger: "blur"
            }],
            type: "select",
            dicUrl: "/api/ManageGradeCourse/GetSchoolStageSelect",
            cascader: ['njInitId'],
            props: {
              label: "title",
              value: "id"
            },
          },
          {
            label: "年级",
            prop: "njInitId",
            type: "select",
            search: false,
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择年级",
              trigger: "blur"
            }],
            hide: true,
            dicUrl: "/api/ManageGradeCourse/GetGradeSelect?parentId={{key}}",
            props: {
              label: "title",
              value: "id"
            },
            change: ({value}) => {
              this.njInitIdTypeChange(value);
            }
            // dataT type: "number"
          },
          {
            label: "课程",
            prop: "kcId",
            search: false,
            rules: [{
              required: true,
              message: "请选择课程",
              trigger: "blur"
            }],
            type: "select",
            dicUrl: "/api/ManageGradeCourse/GetCourseSelect?schoolId={{key}}",
            // cascader: ['njId'],
            props: {
              label: "kcName",
              value: "id"
            },
            dicData: []

          },
          // {
          //   label: "年级名称",
          //   prop: "njName",
          //   rules: [{
          //     required: true,
          //     message: "请输入年级名称",
          //     trigger: "blur"
          //   }],
          //   // type: "number",
          // },
          {
            label: "周课时数",
            prop: "zkss",
            rules: [{
              required: true,
              message: "请输入周课时数",
              trigger: "blur"
            }],
            type: "number",
          },
        ]
      },
      data: [],
      schoolId: '',
      jdId: ''
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.nj_add, false),
        viewBtn: this.vaildData(this.permission.nj_view, false),
        delBtn: this.vaildData(this.permission.nj_delete, false),
        editBtn: this.vaildData(this.permission.nj_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    var userinfo = getStore({
      name: "userInfo",
    });
    getBjTree({jsId: userinfo.relateId}).then(res => {
      this.treeData = res.data.data;
    });
  },
  methods: { // 切换学校
    schoolTypeChange(v) {
      console.log(v)
      this.schoolId = v
      const school = this.findObject(this.option.column, "kcId");
      school.dicData = [];
    },
    // 切换年级
    njInitIdTypeChange(v) {
      if (this.schoolId !== "" && this.schoolId !== null && this.schoolId !== undefined) {
        let params = {
          schoolId: this.schoolId,
          njInitId: v
        }
        course(params).then(res => {
          const school = this.findObject(this.option.column, "kcId");
          school.dicData = res.data.data;
          console.log('1111111111111', res)
        });
      }
    },
    nodeClick(data) {
      console.log(data)
      if (data.hasChildren) {
        this.schoolId = data.schoolId
        this.jdId = data.id
        this.onLoad(this.page);
      } else {
        this.data = []
      }
    },
    rowSave(row, done, loading) {
      row.id = row.id === '' ? '0' : row.id
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit"].includes(type)) {
        getDetail({id: this.form.id}).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      // if (this.schoolId == '' || this.jdId == '') {
      //   this.loading = false;
      //   // this.selectionClear();
      //   return 0
      // }
      this.loading = true;
      // params.schoolId = this.schoolId === '' ? '0' : this.schoolId; //学校id
      // params.njInitId = this.jdId === '' ? '0' : this.jdId //年级id

      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        data.records.forEach(item => {
          item.kcId = item.kcName
        })
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
      // const school = this.findObject(this.option.column, "schoolId");
      // school.hide = true;

    }
  }
};
</script>

<style>
</style>
